package com.wondersgroup.fjzlpt.yyzyzx.dao.impl.yyzygl.yysz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.quick.framework.boot.repository.sql.dialect.DialectProxy;
import com.wondersgroup.fjzlpt.yyzyzx.util.ControllerUtils;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzygl.yysz.HcgzDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;

/**
 * 互斥规则Dao
 * @author gzb   
 * 2017年8月8日
 */
@Repository
public class HcgzDaoImpl extends BaseJTDAOImpl implements HcgzDao {
	@Autowired
	private DialectProxy dialectProxy;

	@Override
	@Autowired
	@Qualifier("TB_YYZY_HCGZ")
	public void setTb(TableBean tb){
		super.setTb(tb);
	}

	/**
	 * 分页查询 互斥规则
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryPage(Map map) {
		ControllerUtils.pageFormat(map);
		JSONObject sqlObj = this.getQueryPageSql(map);
		
		List sqlParams = sqlObj.getJSONArray("sqlParams");
		PageQuerySQLBean pageBean = super.checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), "order by ZT", sqlParams.toArray());
		
		return super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 分页语句拼接
	 * @param map
	 * @return
	 */
	private JSONObject getQueryPageSql(Map<String,String> map){
		String groupFunName = dialectProxy.getRow2ColumnFuncName(this.getJdbcTemp().getDataSource());
		JSONObject result = new JSONObject();
		
		StringBuffer sqlBuf = new StringBuffer();
		//A.*
		sqlBuf.append("select A.GZBH,A.GZMC,A.PTJGID,A.YLJGMC,A.YLJGDM,A.KSDM,A.KSMC,A.XMDM,A.XMMC,A.GZMS,A.PCSJ,A.ZT,");
		//被排斥项目名称
		if("GROUP_CONCAT".equals(groupFunName.toUpperCase())){
			sqlBuf.append("GROUP_CONCAT(D.XMMC) BPCXMMC ");
		}else if("LISTAGG".equals(groupFunName.toUpperCase())){
			sqlBuf.append("listagg(D.XMMC,',') within group (order by D.XMDM) BPCXMMC ");
		}else{
			throw new RuntimeException("无法确定分组函数");
		}
		sqlBuf.append("from ").append(this.tb.getTableName());
		sqlBuf.append(" A LEFT JOIN TB_ZD_YLJGXX B ON A.PTJGID = B.PTJGID \n");
		sqlBuf.append("left join TB_YYZY_BHCXM C on A.GZBH=C.GZBH  \n");
		sqlBuf.append("left join TB_ZY_JCXM D on C.XMDM=D.XMDM where 1=1 \n");

		List sqlParams = new ArrayList();
		
		//平台机构ID
		String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and A.PTJGID = ? ");
			sqlParams.add(value);
		}
		//科室代码
		value = map.get("KSDM");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and A.KSDM = ? ");
			sqlParams.add(value);
		}
		//行政区划代码
		value = map.get("XZQHDM");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and B.XZQHDM LIKE ? ");
			sqlParams.add(value + "%");
		}
		sqlBuf.append("group by A.GZBH,A.GZMC,A.PTJGID,A.YLJGMC,A.YLJGDM,A.KSDM,A.KSMC,A.XMDM,A.XMMC,A.GZMS,A.PCSJ,A.ZT ");
		result.put("sql", sqlBuf.toString());
		result.put("sqlParams", sqlParams);
		return result;
	}
}
